﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmCheckBox : Form
    {
        public FrmCheckBox()
        {
            InitializeComponent();
        }

     
        private void FrmCheckBox_Load(object sender, EventArgs e)
        {
            foreach(Control c in panelIntresting.Controls)
            {
                if(c is CheckBox)
                {
                    //代码注册
                    ((CheckBox)c).CheckedChanged += FrmCheckBox_CheckedChanged;
                }
            }
            foreach (Control c in panelRoles.Controls)
            {
                if (c is CheckBox)
                {
                    //代码注册
                    ((CheckBox)c).CheckedChanged += FrmCheckBox1_CheckedChanged;
                }
            }
        }

        private void FrmCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = sender as CheckBox;
            if (c.Checked)
            {
                listRoles.Add(c.Text);
            }
            else
            {
                listRoles.Remove(c.Text);
            }
        }

        List<string> listIntesting = new List<string>();
        List<string> listRoles = new List<string>();
        private void FrmCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = sender as CheckBox;
            if(c.Checked)
            {
                listIntesting.Add(c.Text);
            }
            else
            {
                listIntesting.Remove(c.Text);
            }
        }

        /// <summary>
        ///获取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            string state = chkNormal.Checked ? chkNormal.Text : "冻结";
            string strIntesting = string.Join(",", listIntesting);
            string strRoles = string.Join(",", listRoles);
            lblResult.Text = $"用户：{txtUserName.Text} 他的状态是：{state}; 兴趣是：{strIntesting}; 他的角色是：{strRoles}";
        }

        /// <summary>
        /// 先触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkNormal_CheckedChanged(object sender, EventArgs e)
        {
            //if(chkNormal.Checked)
            //{
            //    MessageBox.Show("正常");

            //}
            //else
            //{
            //    MessageBox.Show("冻结");
            //}
        }

        /// <summary>
        /// 后触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkNormal_CheckStateChanged(object sender, EventArgs e)
        {
            // if(chkNormal.CheckState==CheckState.Checked)
            //{
            //    MessageBox.Show("CheckState正常");
            //}
            // else
            //{
            //    MessageBox.Show("CheckState冻结");
            //}
        }
    }
}
